<?php

namespace Itbbn\ThinkphpModelCreater\lib;


class Base
{
    public static $debug = true;
    public static $isLog = true;


    public static function writeln($obj, $format = "string")
    {
        $str = "";
        if ($format == "string") {
            $str = print_r($obj, true) . "\n";
        }
        if ($format == "json") {
            $str = json_encode($obj, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
        }
        if (Config::instance()->debug) {
            echo $str;
        }
        if (self::$isLog) {
            //Log::record($str, 'debug');
        }
    }

    public static function writeRepeat($obj, $format = "string")
    {
        $str = "";
        if ($format == "string") {
            $str = print_r($obj, true);
        }
        if ($format == "json") {
            $str = json_encode($obj, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
        }
        if (Config::instance()->debug) {
            // 计算进度百分比
            echo "\r" . $str;
            usleep(100000);

        }
        if (self::$isLog) {
            //Log::record($str, 'debug');
        }

    }

    public static function echoPercent($i, $total)
    {
        // 计算进度百分比
        $percent = ($i / $total) * 100;

        // 计算进度条长度（总宽度为50个字符）
        $barLength = 50;
        $filledLength = round($barLength * $i / $total);
        $emptyLength = $barLength - $filledLength;

        // 构建进度条字符串
        $bar = str_repeat('=', $filledLength);
        if ($filledLength < $barLength) {
            $bar .= '>';
            $emptyLength--;
        }
        $bar .= str_repeat(' ', $emptyLength);

        $percent = number_format($percent, 2);
        if (Config::instance()->debug) {
            // 输出进度条（使用\r回到行首）
            echo "\r[$bar] $percent% ($i/$total) ";
        }
    }

}